<?php

namespace app\controller\admin\module\setting;

use app\controller\admin\module\AdminBaseController;
use think\exception\ValidateException;
use think\response\Json;
use think\response\View;

/**
 * 系统设置
 * @auth-name 系统设置
 * @auth-alias setting
 */
class Index extends AdminBaseController
{
    protected string $modelName = 'options';

    /**
     * @auth-name 主页
     * @auth-alias index
     * @return View
     */
    public function index(): View
    {
        return fetch('/admin/setting/index', [
            'options' => getSettingOptionsCache(),
        ]);
    }

    /**
     * 保存
     * @auth-name 保存设置
     * @auth-alias save
     * @auth-request-method post
     * @return Json
     */
    public function save(): Json
    {
        $baseArray = values('post.base');       //基本设置
        $uploaderArray = values('post.uploader');       //上传设置
        $webSiteTitle = $baseArray['web_site_title'] ?? '';    //网站名称
        $webSiteLogo = $baseArray['web_site_logo'];    //LOGO图片
        $webSiteLogoText = $baseArray['web_site_logo_text'];    //LOGO图片(带文字)
        $uploadMode = $uploaderArray['mode'] ?? '';    //上传模式
        $uploadImageExt = $uploaderArray['image_ext'] ?? '';    //上传的图片后缀
        $uploadImageSize = $uploaderArray['image_size'] ?? '';    //图片上传大小限制
        $uploadFileExt = $uploaderArray['file_ext'] ?? '';    //上传的文件后缀
        $uploadFileSize = $uploaderArray['file_size'] ?? '';    //上传大小限制
        try {
            $this->validate([
                'webSiteTitle' => $webSiteTitle,
                'webSiteLogo' => $webSiteLogo,
                'webSiteLogoText' => $webSiteLogoText,

                'uploadMode' => $uploadMode,
                'uploadImageExt' => $uploadImageExt,
                'uploadImageSize' => $uploadImageSize,
                'uploadFileExt' => $uploadFileExt,
                'uploadFileSize' => $uploadFileSize,

            ],[
                'webSiteTitle|网站名称' => ['require', 'max'=> 50],
                'webSiteLogo|LOGO图片' => ['require'],
                'webSiteLogoText|LOGO图片(带文字)' => ['require'],
                'uploadMode|上传模式' => ['require'],
                'uploadImageExt|上传的图片后缀' => ['require'],
                'uploadImageSize|图片上传大小限制' => ['require'],
                'uploadFileExt|上传的文件后缀' => ['require'],
                'uploadFileSize|上传大小限制' => ['require'],
            ]);
            $info = $this->model->getInfo(['mode' => 'setting'], 'id');
            if(!empty($info)){
                $res = $this->model->edit(['mode' => 'setting'], ['config' => json_encode($this->request->post())]);
            }else{
                $res = $this->model->add([
                    'mode' => 'setting',
                    'config' => json_encode($this->request->post()),
                ]);
            }
            if($res){
                putAdminLog("更新系统设置");
                resetSettingOptionsCache();
                return responseMsg('更新成功', 200);
            }
            return responseMsg('更新失败');
        }catch (ValidateException $e){
            return responseMsg($e->getError());
        }
    }
}